﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.EHR.Library;
using ZX.DBModule;
using ZX.BLL;
using System.Collections;
using System.Data;

namespace ZX.WebApp.EHR
{
    public partial class GetSequenceNum : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            //以前的，适用于部门工号分配-----------------------------------------------------
            //try
            //{
            //    //部门为空返回空，部门未分配工号返回-1，成功分配返回工号
             string deptId = this.GetStringFromQueryString("deptId", string.Empty);
            //    if (deptId == string.Empty)
            //    {
            //        HttpContext.Current.Response.Write("-1");
            //        return;
            //    }
            //    //根据部门ID获取部门DeptPath
                string condition = string.Format("DeptID='{0}'", deptId);
                DatDepartment _Dept = ZX.Common.Globals.getEntity("DatDepartment") as DatDepartment;
                ZX.BLL.Extend.BaseBussnies DeptBll = new ZX.BLL.Extend.BaseBussnies(_Dept);
                _Dept = DeptBll.GetEntityByCondition(condition) as DatDepartment;//获取该部门信息
            //    string sequenceNum = deptInfo.DeptPath; //获取DeptPath
            //    //获取员工工号
            //    string num = this.GetSeqNum(sequenceNum);
            //    HttpContext.Current.Response.Write(num);
            //}
            //catch(Exception ex)
            //{
            //    HttpContext.Current.Response.Write("e_" + ex.Message);
            //}
            //-----------------------------------------------------------------
            //目前工号分配为全公司用同一个工号段
            //获取最大工号值
            //ZX.BLL.Extend.BaseBussnies<hrEmployee> hrBll = new BLL.Extend.BaseBussnies<hrEmployee>();
            //DataTable dt = hrBll.GetDataBySqlComm("select MAX(EmployeeCode) from hrEmployee");
            //Int64 seqNum = -1;
            //try
            //{
            //    seqNum = Int64.Parse(dt.Rows[0][0].ToString());
            //}
            //catch
            //{ 
            //}
            ////先判断公司工号配置是否存在
                BaseEntity instance = ZX.Common.Globals.getEntity("Sequence") as BaseEntity;
                ZX.BLL.Extend.BaseBussnies SequenceBll = new ZX.BLL.Extend.BaseBussnies(instance);
                Sequence sequence = SequenceBll.GetEntityByCondition<Sequence>("SequenceName='EmpCode'");
            int numeLen = 0;
            //if (sequence == null)//工号未配置或被删除
            //{
            //    string sql = string.Empty;
            //    Sequence seq = new Sequence();
            //    seq.SequenceID = Guid.NewGuid();
            //    seq.SequenceName = "EmpCode";
            //    seq.NumLength = 6;
            //    numeLen = 6;
            //    seq.Description = "中旭股份工号";
            //    seq.Step = 1;
            //    seq.MaxNum = 999999;
            //    if (seqNum == -1)
            //    {
            //        seq.StartNum = 8000;
            //        seq.CurNum = 8000;
            //    }
            //    else
            //    {
            //        seq.StartNum = seqNum + 1;
            //        seq.CurNum = seqNum + 1;
            //    }
            //    SequenceBll.Insert(seq);//未配置就先配置工号
            //}
            //else//工号已配置
            //{
            //    if (seqNum != -1)
            //    {
            //        //如果当前工号值比公司工号最大值小，则当前值为工号最大值加1
            //        if (sequence.CurNum < seqNum)
            //        {
            //            sequence.CurNum = seqNum + 1;
            //            SequenceBll.Update(sequence);
            //        }
            //    }
            //    else
            //    {
            //        sequence.CurNum = 8000;
            //        SequenceBll.Update(sequence);.0
            //    }
            //    numeLen = sequence.NumLength;
            //}
            //生成工号
            numeLen = sequence.NumLength;
            Int64 num = IDMaker.GetSequenceNum("EmpCode");     
            string sequenceNum = num.ToString();
            if (numeLen > num.ToString().Length)
            {
                for (int i = 0; i < numeLen - num.ToString().Length; i++)
                {
                    sequenceNum = "0" + sequenceNum;
                }
            }
            //string deptId = this.GetStringFromQueryString("deptId", string.Empty);

            HttpContext.Current.Response.Write(_Dept.HelpCode+sequenceNum);
		}

        /// <summary>
        /// 根据部门deptPath获取部门员工工号
        /// </summary>
        /// <param name="deptPath"></param>
        /// <returns></returns>
        private string GetSeqNum(string deptPath)
        {
            try
            {
                string condition = string.Format("SequenceName='{0}'", deptPath);
                BaseEntity instance = ZX.Common.Globals.getEntity("Sequence") as BaseEntity;
                ZX.BLL.Extend.BaseBussnies SequenceBll = new ZX.BLL.Extend.BaseBussnies(instance);
                IList<BaseEntity> list = SequenceBll.GetEntitysByCondition(condition);
                if (list == null || list.Count == 0)//该部门未分配或由上级部门分配 
                {
                    int index = deptPath.LastIndexOf("-");
                    if (index < 1) //该部门没有上级部门
                    {
                        return "-1";
                    }
                    else //有上级部门，寻找上级部门工号段
                    {
                        string parentDeptPath = deptPath.Substring(0, index);
                        return GetSeqNum(parentDeptPath);
                    }
                }
                else //该部门已分配工号段
                {
                    //工号长度
                    ZX.DBModule.Sequence tempSysSequence = list[0] as ZX.DBModule.Sequence;
                    int numeLen = tempSysSequence.NumLength;
                    //生成工号
                    Int64 num = IDMaker.GetSequenceNum(deptPath);
                    string sequenceNum = num.ToString();
                    if (numeLen > num.ToString().Length)
                    {
                        for (int i = 0; i < numeLen - num.ToString().Length; i++)
                        {
                            sequenceNum = "0" + sequenceNum;
                        }
                    }
                    return sequenceNum; //正确分配工号
                }
            }
            catch (Exception ex)
            {
                return "e_" + ex.Message;
            }
        }
	}
}